package club.xyes.zkh.ecommerce.commons.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.Table;
import java.math.BigDecimal;

/**
 * Create by 郭文梁 2019/4/18 0018 14:15
 * ShopMembershipLevel
 * 商铺会员等级实体类
 *
 * @author 郭文梁
 * @data 2019/4/18 0018
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Table(name = "t_shop_membership_level")
public class ShopMembershipLevel extends BaseEntity {
    /**
     * 状态 可用
     */
    public static final int STATE_ENABLE = 0x01;
    /**
     * 状态 不可用
     */
    public static final int STATE_DISABLE = 0x02;
    /**
     * 消费金额和消费次数同时满足
     */
    public static final int CONDITION_MODE_BOTH = 0x01;
    /**
     * 满足消费金额或消费次数
     */
    public static final int CONDITION_MODE_OR = 0x02;
    /**
     * 满足消费金额
     */
    public static final int CONDITION_MODE_ONLY_FEE = 0x03;
    /**
     * 满足消费次数
     */
    public static final int CONDITION_MODE_ONLY_TIMES = 0x04;

    /**
     * 商铺会员ID
     */
    @Column(name = "shop_membership_id", length = 10, nullable = false)
    private Integer shopMembershipId;
    /**
     * 关联的会员定义对象
     */
    private ShopMembership membership;
    /**
     * 等级名称
     */
    @Column(name = "name", length = 100, nullable = false)
    private String name;
    /**
     * 等级值
     */
    @Column(name = "level_value", length = 5, nullable = false)
    private Integer levelValue;
    /**
     * 条件组合方式
     */
    @Column(name = "condition_mode", length = 2, nullable = false)
    private Integer conditionMode;
    /**
     * 使用条件：最低消费金额
     */
    @Column(name = "condition_amount")
    private String conditionAmount;
    /**
     * 使用条件：消费次数
     */
    @Column(name = "condition_times", length = 5)
    private Integer conditionTimes;
    /**
     * 折扣率
     */
    @Column(name = "discount_rate", length = 100, nullable = false)
    private String discountRate;
    /**
     * 状态
     */
    @Column(name = "state", length = 2, nullable = false)
    private Integer state;

    /**
     * 获取描述文本
     *
     * @return 描述文本
     */
    public String toDescriptionText() {
        int times = conditionTimes == null ? 0 : conditionTimes;
        int amount = 0;
        String rate = "10";
        try {
            rate = new BigDecimal(discountRate).multiply(BigDecimal.valueOf(10)).toString();
        } catch (Exception e) {
            //do nothing
        }
        try {
            amount = Integer.parseInt(conditionAmount) / 100;
        } catch (Exception e) {
            // do nothing
        }
        String res;
        switch (this.conditionMode) {
            case CONDITION_MODE_BOTH:
                res = String.format("消费满%s元并且支付%s笔交易， 会员折扣%s折", amount, times, rate);
                break;
            case CONDITION_MODE_OR:
                res = String.format("消费满%s元或者支付%s笔交易， 会员折扣%s折", amount, times, rate);
                break;
            case CONDITION_MODE_ONLY_FEE:
                res = String.format("消费满%s元 会员折扣%s折", amount, rate);
                break;
            case CONDITION_MODE_ONLY_TIMES:
                res = String.format("支付%s笔交易， 会员折扣%s折", times, rate);
                break;
            default:
                throw new IllegalStateException("Unexpect ConditionMode " + conditionMode);
        }
        return res;
    }
}
